import Demo1.HashBuck;
import Demo1.HashBuck2;

import java.util.*;
class Student{
    public String id;

    public Student(String id){
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

public class test {
    public static void main(String[] args) {
        char[] ch = new char[]{'a','b','c'};
        String s1 = new String(ch);
        // s1.intern();
        // 让s1所指的对象入池,如果在常量池中存在就不入池
        String s2 = "abc";
        System.out.println(s1 == s2);// false 有s1.intern()就是true
    }
    public static void main9(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        String s3 = new String("hello");
        String s4 = new String("hello");

        System.out.println(s1 == s2);// true
        System.out.println(s1 == s3);// false
        System.out.println(s3 == s4);// false
    }
    public List<String> topKFrequent(String[] words, int k) {
        // 1. 统计单词出现的次数
        Map<String,Integer> map = new HashMap<>();
        for(String s : words){
            if(!map.containsKey(s)){
                map.put(s,1);
            }else{
                int val = map.get(s);
                map.put(s,val+1);
            }
        }
        // 2. 把单词和出现的次数当做一个整体放入小根堆中
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>(
                new Comparator<Map.Entry<String,Integer>>(){
        public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2){
            // 放元素的时候,如果频率相同,我们转变为大根堆 -> 按照单词的字典序进行排序
            if(o1.getValue().compareTo(o2.getValue()) == 0){
                return o2.getKey().compareTo(o1.getKey());
            }
            return o1.getValue().compareTo(o2.getValue());
        }
        });

        for(Map.Entry<String,Integer> entry : map.entrySet()){
            if(minHeap.size() < k){
                // 没有放满小根堆
                minHeap.add(entry);
            }else{
                // 放满了和堆顶元素比较大小
                // 如果比堆顶元素还大,就入堆
                int v = minHeap.peek().getValue();
                if(v < entry.getValue()){
                    minHeap.poll();
                    minHeap.offer(entry);
                }else{
                    // 出现频率相同,比较字典序大小
                    if(v == entry.getValue()){
                        if(minHeap.peek().getKey().compareTo(entry.getKey()) > 0){
                            minHeap.poll();
                            minHeap.offer(entry);
                        }
                    }
                }
            }
        }

        // 2 3 4 -> 4 3 2
        List<String> arr = new ArrayList<>();
        for(int i = 0;i < k;i++){
            Map.Entry<String,Integer> top = minHeap.poll();
            arr.add(top.getKey());
        }
        // 逆置
        Collections.reverse(arr);
        return arr;
    }
    public static void main8(String[] args) {
        // 统计6个数中数字出现的次数
        int[] array = {1,1,2,2,3,3};
        Map<Integer,Integer> map = new HashMap<>();

        for(int i = 0;i < array.length;i++){
            if(!map.containsKey(array[i])){
                map.put(array[i],1);
            }else{
                int k = map.get(array[i]);
                k++;
                map.put(array[i],k);
            }
        }

        System.out.println(map);

        for(Map.Entry<Integer,Integer> entry : map.entrySet()){
            System.out.println("key:" + entry.getKey() + " val:" + entry.getValue());
        }
    }
    public static void main7(String[] args) {
        Student student1 = new Student("121212");
        Student student2 = new Student("121212");

        HashBuck2<Student,Integer> hashBuck = new HashBuck2<>();
        hashBuck.put(student1,11);
        System.out.println(hashBuck.get(student2));
    }
    public static void main6(String[] args) {
        Student student1 = new Student("121212");
        Student student2 = new Student("121212");
        System.out.println(student1.hashCode());
        System.out.println(student2.hashCode());

        HashMap<Student,Integer> hashMap2 = new HashMap<>();
        hashMap2.put(student1,11);

        System.out.println(hashMap2.get(student2));
    }
    public static void main5(String[] args) {
        HashMap<String,Integer> hashMap = new HashMap<>();
        hashMap.put("hello",2);
        hashMap.put("abcde",10);
        hashMap.put("abc",11);

        Integer val = hashMap.get("hello");
        System.out.println(val);

        // 遍历map
        System.out.println(hashMap);

        for(Map.Entry<String,Integer> entry : hashMap.entrySet()){
            System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
        }
        // Map不支持迭代器遍历,Set支持迭代器遍历
        // 可以将Map转化为Set进行迭代器遍历

        // HashMap<Student,Integer> hashMap1 = new HashMap<>();
        // hashMap1.put(new Student(),2);
        // hashMap1.put(new Student(),2);
        // hashMap1.put(null,2);

        // TreeMap<Student,Integer> hashMap2 = new TreeMap<>();
        // hashMap2.put(new Student(),3);
        // hashMap2.put(new Student(),3);
        // Sutdent不能进行比较

        // set可以去重,Set的底层是HashMap
        // 每次存储元素的时候,默认的value都是一个Object对象
        HashSet<String> set = new HashSet<>();
        set.add("hello");
        set.add("world");
        set.add("hello");
        System.out.println(set);
    }
    public static void main4(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(11,22);
        hashBuck.put(33,55);
        hashBuck.put(66,99);

        System.out.println(hashBuck.get(66));
    }
    public static void main3(String[] args) {
        Set<String> set = new TreeSet<>();
        set.add("push");
        set.add("hello");
        set.add("world");
        // set是无序的
        System.out.println(set);

        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
    public static void main2(String[] args) {
        Map<String,Integer> map = new TreeMap<>();// 底层是红黑树,查找的时间复杂度O(N*logN)
        // 插入元素
        map.put("push",3);// push出现了3次

        // 获取元素,给定一个key值可以获取它的value值
        Integer val = map.get("push");
        Integer val1 = map.get("aaa");// null
        // 获取val值,如果没有这个值,返回一个默认值
        Integer val2 = map.getOrDefault("bbb",99999);
        System.out.println(val);

        // 删除key值
        // map.remove("push");

        // 把所有的key放入一个集合中
        Set<String> set = map.keySet();
        System.out.println(set);

        // 获取values中的所有值
        ArrayList<Integer> value = new ArrayList(map.values());
        System.out.println(value);

        // 把Map.Entry<String,Integer>当做Set中的一个节点
        // map.entrySet()用于获取这种节点
        Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
        for(Map.Entry<String,Integer> entry : entrySet){
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
        // boolean map.containsKey("push"); 判断是否含有key
        // boolean map.containsValue(3); 判断是否含有value


        Map<String,Integer> map1 = new HashMap<>();// 底层是哈希表,查找的时间复杂度O(1)
        // 哈希表 = 数组 + 链表 + 红黑树

    }
    public static void main1(String[] args) {
        binarySearchTree binarysearchtree = new binarySearchTree();
        int[] array = {12,3,2,1,23,8};
        for(int i = 0;i < array.length;i++){
            binarysearchtree.insert(array[i]);
        }

        System.out.println(binarysearchtree.search(8));
        System.out.println(binarysearchtree.search(13));
    }
}
